<?php
/**
 * @version		$Id: orders.php 7 2011-12-22 13:48:09Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelOrders extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'OrderId', 'a.OrderId',
				'OrderClient', 'a.OrderClient', 'client',
				'OrderDate', 'a.OrderDate',
				'OrderProduct', 'a.OrderProduct',
				'OrderBillingCycle', 'a.OrderBillingCycle', 'billing_cycle',
				'OrderRecurringCost', 'a.OrderRecurringCost',	
				'OrderDomain', 'a.OrderDomain',
				'OrderDomainType', 'a.OrderDomainType',
				'OrderStatus', 'a.OrderStatus',
				'OrderCoupon', 'a.OrderCoupon',
				'OrderIp', 'a.OrderIp',
				'OrderAccount', 'a.OrderAccount',
				'OrderNote', 'a.OrderNote'
			);
		}
		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		$clientId = $app->getUserStateFromRequest($this->context.'.filter.client_id', 'filter_client_id');
		$this->setState('filter.client_id', $clientId);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);
		
		parent::populateState('a.OrderId', 'desc');						
	}
	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);	
		$query->select($this->getState('list.select', 'a.*'));		
		$query->from('#__almh_orders AS a');
		
		$query->select('c.ClientName AS client');
		$query->join('LEFT', '#__almh_clients AS c ON c.ClientId = a.OrderClient');
		
		$query->select('b.BillcycleName AS billing_cycle');
		$query->join('LEFT', '#__almh_billingcycles AS b ON b.BillcycleMonths = a.OrderBillingCycle');
		
		$query->select('ac.WhmaccDomain AS account');
		$query->join('LEFT', '#__almh_whmaccounts AS ac ON ac.WhmaccId = a.OrderAccount');
		
		$query->select('s.WhmaccStatus AS acc_status');
		$query->join('LEFT', '#__almh_whmaccounts AS s ON s.WhmaccId = a.OrderAccount');
		
		
		$clientId = $this->getState('filter.client_id');
		if (is_numeric($clientId)) {
			$query->where('a.OrderClient = '.$clientId);
		}
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.OrderStatus = '.$statusId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'OrderProduct:') === 0) {
				$query->where('a.OrderProduct = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.OrderProduct LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.OrderId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	public function getClient() 
	{
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('c.ClientId AS value, c.ClientName AS text');
		$query->from('#__almh_clients AS c');
		$query->join('INNER', '#__almh_orders AS o ON o.OrderClient = c.ClientId');
		$query->group('c.ClientId');
		$query->order('c.ClientName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
	
	public function orderData()
	{
		
		$db	= JFactory::getDbo();
	 	$pk = JRequest::getInt('OrderId');
	 	
	 	$query	= $db->getQuery(true);
	 	$query->select('a.*');
	 	$query->from('#__almh_orders AS a');
 		$query->where('a.OrderId ='.$pk);	
 		$db->setQuery($query);			
			if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}
			$this->orderData = $db->loadObject();
		
			return $this->orderData;
	}	
	
	public function createInvoice()
	{
		$db	= JFactory::getDbo();
	 	$pk = JRequest::getInt('OrderId');	
		$orderData = $this->orderData;
			
			if ($orderData->OrderDomain == null) {			 
				$product = $orderData->OrderProduct;
			} else {
				$product = "$orderData->OrderProduct ($orderData->OrderDomain)";
			}
 		
		$query	= $db->getQuery(true);
	 	$query->select('InvoiceId, InvoiceOrder, InvoiceDue');
	 	$query->from('#__almh_invoices');
 		$query->where('InvoiceOrder ='.$pk);
 		$query->order('InvoiceId DESC LIMIT 1');
 		$db->setQuery($query);	
			if (!$db->query()) {
					JError::raiseError(500, $db->getErrorMsg());
				}		
			$invoiceData = $db->loadObject();
		
		if(empty($invoiceData->InvoiceOrder)) {
			$duedate = $orderData->OrderDate;
			$cycle = "0";
		} else {
			$duedate = $invoiceData->InvoiceDue;
			$cycle =  $orderData->OrderBillingCycle;
		}
				
		$db = JFactory::getDBO();	
	 	$query	= $db->getQuery(true);
	 	$query = "INSERT INTO #__almh_invoices 
	 	(InvoiceOrder, InvoiceClient, InvoiceDate, InvoiceDue, InvoiceProduct, InvoiceAmount) 
	 	VALUES 
	 	('$pk', '$orderData->OrderClient', NOW(), DATE_ADD('$duedate', INTERVAL '$cycle' MONTH), '$product', '$orderData->OrderRecurringAmount')";
		$db->setQuery($query);
 		$db->query();	
 		
 		$inform = send_email('invoice_created',$orderData->OrderClient);
		
		return true;	
	}
	
	public function suspendAccount()
	{		
		$db	= JFactory::getDbo();
	 	$pk = JRequest::getInt('OrderId'); 	
		$query	= $db->getQuery(true);	
		$query->select('*');
		$query->from('#__almh_whmaccounts');
		$query->where('WhmaccId = '.$this->orderData->OrderAccount);
		$db->setQuery($query);
		$db->setQuery((string) $query);		
			if ($db->getErrorNum()) {
				JError::raiseWarning(500, $db->getErrorMsg());
				return false;
			}
		$account = $db->loadObject();
			
		$suspend = curldata($account->WhmaccServer,suspendacc($account->WhmaccId));	
		$inform = send_email('account_suspended',$orderData->OrderClient);
		$msg = $suspend;
		$app = JFactory::getApplication();
		$urlredirect = "index.php?option=com_almhoster&view=orders";
		$app->redirect( $urlredirect, $msg );	
	}	
	
	public function unsuspendAccount()
	{
		
		$db	= JFactory::getDbo();
	 	$pk = JRequest::getInt('OrderId');
	 	
		$query	= $db->getQuery(true);	
		$query->select('*');
		$query->from('#__almh_whmaccounts');
		$query->where('WhmaccId = '.$this->orderData->OrderAccount);
		$db->setQuery($query);
		$db->setQuery((string) $query);		
			if ($db->getErrorNum()) {
				JError::raiseWarning(500, $db->getErrorMsg());
				return false;
			}
		$account = $db->loadObject();		
		$unsuspend = curldata($account->WhmaccServer,unsuspendacc($account->WhmaccId));	
		$inform = send_email('hosting_unsuspended',$orderData->OrderClient);
		$msg = $unsuspend;
		$app = JFactory::getApplication();
		$urlredirect = "index.php?option=com_almhoster&view=orders";
		$app->redirect( $urlredirect, $msg );
	}
}
?>